﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
///OrderList 的摘要说明
/// </summary>
public class OrderList
{
	public OrderList()
	{		
        this.OrderDetails = new List<OrderDetail>();

        this.OnSubmiting += delegate { };

        this.OnSubmited += delegate { };

        this.OnEachOrderDetailSubmiting += delegate { };
        this.OnEachOrderDetailSubmited += delegate { };
	}

    public List<OrderDetail> OrderDetails { get; set; }
    public int ID { get; set; }

    public string ReciveName { get; set; }
    public string BuyUserPK { get; set; }
    public string OrderID { get; set; }
    public string Address { get; set; }
    public string PostCode { get; set; }
    public string Phone { get; set; }
    public string Mobile { get; set; }

    public string Province { get; set; }
    public string City { get; set; }
    public string Zone { get; set; }
    public int ProvinceID { get; set; }
    public int CityID { get; set; }
    public int ZoneID { get; set; }

    public string PostWay { get; set; }
    public float PostWayNeedMoney { get; set; }

    public string PayWay { get; set; }
    public string PostTime { get; set; }

    public string UserMemo { get; set; }

    public float AllProductPrice { get; set; }
    public float AllProductPriceAfaterDiscount { get; set; }
    public float AllOrderPrice { get; set; }
    public int AddJifen { get; set; }
    public int NeedJifen { get; set; }
    public int AllBuyCount { get; set; }

    public float Discount { get; set; }

    public void ShopCarAddToOrderList<T>(ShopCar<T> shopcar) where T : IProduct, new()
    {
        shopcar.TransToOrderList(shopcar, this);

        foreach (ShopCarItem<T> product in shopcar.AllProducts)
        {
            OrderDetail od = new OrderDetail();
            od.BuyCount = product.BuyCount;
            shopcar.TransToOrderDetail(product.Item, od);
            this.OrderDetails.Add(od);
        }
        
    }

    public delegate void OrderListSubmitingEventHandler(OrderList sender, OrderListSubmitEventArgs args);
    public event OrderListSubmitingEventHandler OnSubmiting;

    public delegate void OrderListSubmitedEventHandler(OrderList sender, string OrderListPkValueInDB);
    public event OrderListSubmitedEventHandler OnSubmited;

    public event OrderDetail.OrderDetailSubmitingEventHandler OnEachOrderDetailSubmiting;
    public event OrderDetail.OrderDetailSubmitedEventHandler OnEachOrderDetailSubmited;

    public void SubmitToDB()
    {
        this.AllProductPrice = this.OrderDetails.Sum(x => x.SinglePrice * x.BuyCount);
        this.AllProductPriceAfaterDiscount = this.AllProductPrice * this.Discount;
        this.AllBuyCount = this.OrderDetails.Sum(x => x.BuyCount);

        OrderListSubmitEventArgs submitingArgs = new OrderListSubmitEventArgs();
        submitingArgs.OrderList = this;
        OnSubmiting(this, submitingArgs);
        if (submitingArgs.Cancel) { return; }
        
        foreach (OrderDetail od in this.OrderDetails)
        {            
            od.OnSubmiting+=this.OnEachOrderDetailSubmiting;
            od.OnSubmited += this.OnEachOrderDetailSubmited;
            od.SubmitToDB(submitingArgs.OrderListPKValueInDB);
        }

        OnSubmited(this, submitingArgs.OrderListPKValueInDB);
    }
}
